package com.csu.cphone.controller;


import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import com.csu.common.vo.ResultVo;
import com.csu.cphone.pojo.PhoneBrandPojo;
import com.csu.cphone.service.PhoneService;
import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;
import com.wordnik.swagger.annotations.ApiParam;

/**
 * @项目名称：cphone
 * @类名称：PhoneController
 * @类描述：
 * @创建人：Tangc
 * @创建时间：2017年9月13日 下午4:09:53
 * @company:
 */
@Controller
@RequestMapping("/phone")
@Api(value = "phone", description = "手机信息")
public class PhoneController
{

    @Autowired
    private PhoneService phoneService;


    @ResponseBody
    @ApiOperation(value = "获取手机品牌", notes = "获取手机品牌列表")
    @RequestMapping(value = "/getBrands", method = RequestMethod.GET, produces = { "application/json; charset=utf-8" })
    public ResultVo<List<PhoneBrandPojo>> getBrands(HttpServletRequest request, HttpServletResponse response)
    {
        ResultVo<List<PhoneBrandPojo>> result = new ResultVo<List<PhoneBrandPojo>>();

        List<PhoneBrandPojo> brandList = phoneService.getBrandList();
        result.setData(brandList);
        return result;
    }
   

    @ResponseBody
    @ApiOperation(value = "添加品牌", notes = "添加手机品牌")
    @RequestMapping(value = "/addPhoneBrand", method =  {RequestMethod.POST,RequestMethod.GET}, produces = { "application/json; charset=utf-8" })
    public ResultVo<PhoneBrandPojo> addPhoneBrand(HttpServletRequest request, HttpServletResponse response,
            @ApiParam(value = "品牌名称") @RequestParam(value = "brandName", required = true) String brandName,
            @ApiParam(value = "图片路径") @RequestParam(value = "url", required = true) String url)
    {
        ResultVo<PhoneBrandPojo> result = new ResultVo<PhoneBrandPojo>();
        PhoneBrandPojo phoneBrand = phoneService.addBrand(brandName, url);
        result.setData(phoneBrand);
        return result;
    }

    
    @ResponseBody
    @ApiOperation(value = "上传图片", notes = "获得图片路径")
    @RequestMapping(value = "/uploadFile", method =  {RequestMethod.POST,RequestMethod.GET})
    public ResultVo<String>  uploadFile(
            HttpServletRequest request,
            HttpServletResponse response,
            @ApiParam(value = "file对象") @RequestParam(value = "productFile", required = true) CommonsMultipartFile productFile)
            throws IOException
    {
        ResultVo<String> result = new ResultVo<String>();
        
        if (request.getCharacterEncoding() == null)
        {
            request.setCharacterEncoding("UTF-8");// 你的编码格式
        }

        String requestUrl = phoneService.wirteFile(productFile);

        result.setData(requestUrl);
        return result;
    }

    
    
}
